package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.AdWmSensitiveMapper;
import com.heima.wemedia.service.AdWmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author lzq
 * @data 2023/5/9 11:49
 */
@Service
@Slf4j
public class AdWmSensitiveServiceImpl extends ServiceImpl<AdWmSensitiveMapper, WmSensitive> implements AdWmSensitiveService {
    /**
     * 敏感词列表查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //参数校验
        dto.checkParam();
        //分页构造器
        Page<WmSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        //条件构造器
        LambdaQueryWrapper<WmSensitive> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(dto.getName()),WmSensitive::getSensitives,dto.getName());
        lqw.orderByDesc(WmSensitive::getCreatedTime);
        //查询
        page(page,lqw);
        //返回数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ResponseResult del(Integer id) {
        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        redisTemplate.delete("wmSensitives");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
